package com.chenglai.seckill.rabbitmq;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.seckill.dao.GroupOnDetailMapper;
import com.chenglai.seckill.dao.GroupOnInfoMapper;
import com.chenglai.seckill.dao.SeckillUserMapper;
import com.chenglai.seckill.entity.MainOrder;
import com.chenglai.seckill.entity.Order;
import com.chenglai.seckill.feign.OrderService;
import com.chenglai.seckill.pojo.GroupOnDetail;
import com.chenglai.seckill.pojo.GroupOnInfo;
import com.chenglai.seckill.pojo.SeckillUser;
import com.chenglai.seckill.service.GroupOnInfoService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 类描述：MQ客户端
 *
 * @author rjh
 * @date 2023/11/7 9:50
 */
@Component

public class MqClient {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SeckillUserMapper seckillUserMapper;

    @Resource
    private OrderService orderService;

    @Resource
    private GroupOnInfoService groupOnInfoService;

    @Resource
    private GroupOnInfoMapper groupOnInfoMapper;

    @Resource
    private GroupOnDetailMapper groupOnDetailMapper;

    @RabbitHandler
    @RabbitListener(queues = "seckillQueue")
    public void recive(Map<String, Object> data) {
        System.out.println("收到秒杀消息");
        Integer seckillId = (Integer) data.get("seckillId");
        Integer userId = (Integer) data.get("userId");
        Integer goodsId = (Integer) data.get("goodsId");
        Integer shopId = (Integer) data.get("shopId");
        Double price = (Double) data.get("price");
        Integer number = (Integer) data.get("number");
        String orderNum = String.valueOf(new Date().getTime());
        //生成订单
        MainOrder mainOrder = new MainOrder();
        mainOrder.setShopId(shopId);
        mainOrder.setUserId(userId);
        mainOrder.setOrderNum(orderNum);
        mainOrder.setOrderStatus(0);
        mainOrder.setOrderTime(DateFormatHelper.currentTimes());
        mainOrder.setTotalSum(price * number);
        mainOrder.setMoNote("秒杀订单");
        List<Order> orderList = new ArrayList<>();
        Order order = new Order();
        order.setOrderNum(orderNum);
        order.setProductId(goodsId);
        order.setProductNum(number);
        order.setProductPrice(price);
        order.setOrderSum(price * number);
        order.setOrderNote("秒杀商品");
        order.setShopId(shopId);
        orderList.add(order);
        mainOrder.setOrderList(orderList);



        //调用订单服务
        orderService.addMainOrder(mainOrder, orderList);
        //生成user记录
        SeckillUser seckillUser = new SeckillUser();
        seckillUser.setSeckillId(seckillId);
        seckillUser.setUserId(userId);
        seckillUser.setGoodsId(goodsId);
        seckillUserMapper.insert(seckillUser);

        //存入redis
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("seckill_s_u:" + seckillId + ":" + userId + ":" + goodsId, true);
    }

    @RabbitHandler
    @RabbitListener(queues = "groupNoQueue")
    public void groupNoRecive(Map<String, Object> data) {
        System.out.println("收到秒杀消息");

        String orderNum = (String) data.get("orderNum");
        Integer userId = (Integer) data.get("userId");
        Integer goodsId = (Integer) data.get("goodsId");
        Integer shopId = (Integer) data.get("shopId");

        //查询该商品是否有团
        Integer groupId = groupOnInfoMapper.queryGroupOnId(goodsId, shopId);
        if (groupId == null) {
            //无团 创建一个团
            //创建主表
            GroupOnInfo groupOnInfo = new GroupOnInfo();
            groupOnInfo.setShopId(shopId);
            groupOnInfo.setGoodsId(goodsId);
            groupOnInfo.setUserId(userId);
            Integer gid = groupOnInfoService.editGroupOnInfo(groupOnInfo);
            //创建子表
            GroupOnDetail detail = new GroupOnDetail();
            detail.setOrderNum(orderNum);
            detail.setGroupId(gid);
            detail.setUserId(userId);
            groupOnDetailMapper.insert(detail);
        } else {
            //有团 加入该团
            //创建子表
            GroupOnDetail detail = new GroupOnDetail();
            detail.setOrderNum(orderNum);
            detail.setGroupId(groupId);
            detail.setUserId(userId);
            groupOnDetailMapper.insert(detail);
            //判断该团人数是否够了，够了修改主表状态
            Integer count = groupOnDetailMapper.detailCount(groupId);
            Integer groupNumber = groupOnDetailMapper.queryGroupNumber(goodsId);
            if (count == groupNumber) {
                groupOnInfoMapper.updateStatus(2, groupId);
            }
        }
    }

}
